package com.feiyi.app.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.feiyi.app.R;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.bean.CommonBean;
import com.feiyi.app.net.HttpMethods;
import com.wang.avi.AVLoadingIndicatorView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * @author wangliantao
 * @time 2018 12 2018/12/8 3:29 PM
 * @email 1346947404@qq.com
 * @package com.feiyi.app.activity
 * @describe： 用户反馈界面
 * In me the tiger sniffs the rose
 */
public class MyFeedback extends BaseActivity {
    @BindView(R.id.et_baoliao_title)
    EditText etBaoliaoTitle;
    @BindView(R.id.et_baoliao_content)
    EditText etBaoliaoContent;



    @BindView(R.id.tv_title_titlebar)
    TextView tvTitlebar;

    private String uid;

    private String sid;

    private String feedContent;

    private String feedTitle;

    private static final String TAG = "main";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myfeedback);
        ButterKnife.bind(this);
        tvTitlebar.setText("留言反馈");
    }

    private void getDatas(){
        uid = spUtils.getString(AppConstant.UID);
        sid = spUtils.getString(AppConstant.SID);
        feedContent = etBaoliaoContent.getText().toString();
        feedTitle = etBaoliaoTitle.getText().toString();
    }
    @OnClick({R.id.iv_back_titlebar,R.id.but_baoliao})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back_titlebar:
                finish();
                break;
            case R.id.but_baoliao:
                getDatas();
                sendFeedback();
                break;
        }
    }

    private void sendFeedback(){

        if (feedTitle.isEmpty()){
            ToastUtils.showShort("反馈标题不能为空");
            return;
        }
        if (feedContent.isEmpty()){
            ToastUtils.showShort("反馈内容不能为空");
            return;
        }
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("uid",uid);
        builder.add("sid",sid);
        builder.add("content",feedContent);
        builder.add("title",feedTitle);
        RequestBody body = builder.build();
        HttpMethods.getInstance().info.feedBack(AppConstant.FEEDBACK,body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(CommonBean commonBean) {
                        if (commonBean.getStatus().getSucceed() == 1){
                            ToastUtils.showShort("反馈成功");
                            finish();
                        }else {
                            ToastUtils.showShort("输入有误，请重新输入");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "反馈失败 "+e.toString() );
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
